package crm.model.teacherPC.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.TreeMap;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import crm.model.teacherPC.Manager;
import crm.model.teacherPC.bannedList.ApplicationBanned;
import crm.model.teacherPC.bannedList.BrowserBanned;
import crm.model.teacherPC.connectList.ConnectStudent;
import crm.model.teacherPC.net.Transfer;



public class XMLReader {
	private final Manager manager;
	private final BrowserBanned browserBannedList;
	private final ApplicationBanned appBannList;
	public XMLReader(Manager manager, BrowserBanned browserBannedList, ApplicationBanned appBannList) {
		this.manager=manager;
		this.browserBannedList = browserBannedList;
		this.appBannList=appBannList;
	}
	public void open(){
		TreeMap<String, ConnectStudent> list = manager.connectingStudent;
		File f = new File("a.xml");
		try {
			XMLInputFactory factory = XMLInputFactory.newInstance();
			XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream(f));
			while(reader.hasNext()){
				int val = reader.next();

				if(val == reader.START_ELEMENT){
					if (reader.getName().getLocalPart().equals("Student")){
						String ip = reader.getAttributeValue(0);
						String info = reader.getAttributeValue(1);
						list.put(ip, new ConnectStudent(ip,info));
					} else if (reader.getName().getLocalPart().equals("Filter")){
						String filterValue = reader.getAttributeValue(0);
						Transfer.filterList.add(filterValue);
					}else if (reader.getName().getLocalPart().equals("Browser")){
						String browserName = reader.getAttributeValue(0);
						boolean browserEnable = Boolean.parseBoolean(reader.getAttributeValue(1));
						browserBannedList.addBrowser(browserName, browserEnable);
					}else if (reader.getName().getLocalPart().equals("Application")){
						String appName = reader.getAttributeValue(0);
						boolean enable = Boolean.parseBoolean(reader.getAttributeValue(1));
						appBannList.addBann(appName, enable);
					}
				}
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (XMLStreamException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
